<?php

defined('SYSPATH') or die('Access error!');

class Statics
{

    private static $instances;
    public $type;
    public $id_arr = array();
    public $operate;

    public static function instance()
    {
        if (!isset(Statics::$instances))
        {
            Statics::$instances = new Statics();
        }
        return Statics::$instances;
    }

    public function update_static($table, $id_arr, $type, $operate = 'add')
    {
        if ($type != '' && Arr::validate_arr($id_arr))
        {
            $this->type = $type;
            $this->id_arr = $id_arr;
            $this->operate = $operate;
            if (Model::factory($table)->update_operate_num($this->get_operate_sql(), $id_arr))
           {
                return TRUE;
           }
        }
        return FALSE;
    }

    public function get_operate_sql()
    {

        if (!empty($this->operate))
        {
            switch ($this->operate)
            {
                case 'add':
                    return $this->type . '=' . $this->type . '+1';
                    break;
                case 'sub':
                    return $this->type . '=' . $this->type . '-1';
                    break;
                default:
                    break;
            }
        }
    }

    public function get_table_by_type($type)
    {
        switch ($type)
        {
            case 'goods':
                return array('table' => 'shop_goods_static', 'key' => 'goods_id');
                break;
            case 'store':
                return array('table' => 'shop_store', 'key' => 'id');
                break;
             case 'news':
                return array('table' => 'news', 'key' => 'id');
                break;
            case 'trade':
                return array('table' => 'trade_post', 'key' => 'id');
                break;
            default:
                return FALSE;
                break;
        }
    }

}